<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $banner = Db::name('banner')->select();
        $goods = Db::name('commodity')->where('type',1)->select();
        return json_encode(['banner'=>$banner,'goods'=>$goods]);
    }
    // 获取换取 openId, sessionKey, unionId
    public function openid(){
        $arrContextOptions=array(
            "ssl"=>array(
                "verify_peer"=>false,
                "verify_peer_name"=>false,
            ),
        );
        $code = $this->request->get('code');
        $appId = 'wx93c33eff87c5cdf7';
        $Secret = '4b790b206fb07492726d371e157afe78';
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appId."&secret=".$Secret."&js_code=".$code."&grant_type=authorization_code";
        $weixin = file_get_contents($url, false, stream_context_create($arrContextOptions));
        $jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
        $array = get_object_vars($jsondecode);//转换成数组
        return json_encode($array);
    }
    // 添加修改用户信息
    public function user(){
        $params = $this->request->get();
        $res = model('People')->add_update_user($params);
        return $res?json_encode(200):json_encode(400);
    }
    // 解密数据
    public function decrypt(){
        require_once VENDOR_PATH . 'decryption/wxBizDataCrypt.php';
        $appid = 'wx93c33eff87c5cdf7';
        $sessionKey = $this->request->post('session_key');
        $encryptedData = $this->request->post('encryptedData');
        $iv = $this->request->post('iv');
        $pc = new \WXBizDataCrypt($appid, $sessionKey);
        $errCode = $pc->decryptData($encryptedData, $iv, $data);
        if ($errCode == 0) {
            return $data;
        } else {
            return $errCode;
        }
    }
    public function phone(){
        $people = model('People')->people($this->request->post('openid'));
        $res = Db::name('people')->where('id',$people['id'])->update(['phone'=>$this->request->post('phone')]);
        return $res?200:400;
    }
    public function im(){
        $people = model('People')->people($this->request->post('openid'));
        $doctorImg = Db::name('config')->where('id',18)->value('value');
        $doctorSum = Db::name('config')->where('id',19)->value('value');
        return json_encode(['doctorImg'=>$doctorImg,'doctorSum'=>$doctorSum]);
    }
    //微信端上传图片
    public function uploadimg()
    {
        $file = request()->file('file');
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                $file = $info->getSaveName();
                $res = "/uploads/".$file;
                return $res;
            }
        }
    }
    public function zxApi($time,$deductContent,$nonce,$card,$start,$end){
        date_default_timezone_set('PRC');
        $data = array(
            'appkey'=>'6S7YK1E5',
            'couponSource'=> '4001',
            'deductRuleType'=> '3',
            'parkCodes'=>'10119032716423002000',
            'deductContent'=> $deductContent,
            'generateObj'=> '1',
            'nonce'=> $nonce,
            'plateNo'=> $card,
            'startTime'=> $start,
            'endTime'=> $end,
            'time'=> $time
        );
        $secret = '7864510C3C9E981C826A3D32CC3F33FA';
        //$data=array_filter($data);
        ksort($data);
        $str='';
        foreach($data as $k=>$v) {
            $str.=$k.'='.$v.'&';
        }
        $str.='secret='.$secret;
//        dump($str);die;
        $strs = strtoupper(md5($str));
        $url = "http://www.hikparking.com:8086/cloud/service/generateCoupon?token=".$strs;
        $res = $this->curl($url,json_encode($data));
        return json_encode($res);
    }
    function curl($url,$data) {
        $headers = array(
            "Content-type:application/json;charset='utf-8'",
            "Content-Length:".strlen($data)
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $tmpInfo = curl_exec($ch);
        $error = curl_errno($ch);
        curl_close($ch);
        if ($error) {
            return $error;
        }else{
            return $tmpInfo;
        }
    }
}
